require 'statistic/json_fields_counter'
require 'utils'

crawl_table = Object.const_get(ARGV[0])

fields_counter = Statistic::JSONFieldsCounter.new crawl_table

fields_counter.update_statistic

sorted = fields_counter.fields_stat.sort {|a,b| b[1] <=> a[1]}
sorted.each {|e| e[1] = e[1] / fields_counter.records_analyzed.to_f * 100.0}

csv = Utils.mat_to_csv sorted
lex = Utils.mat_to_latex sorted

base_file_name = ARGV[0].downcase

File.open("#{Rails.root}/log/#{base_file_name}_fileds_count.log", 'wb') {|f|
  f.write "OK #{fields_counter.records_analyzed} FAIL #{fields_counter.records_analyzed_fail.length} ERRORS #{fields_counter.errors.length}"
  fields_counter.errors.each { |e|
    f.write e[0]
    f.write e[1]
  }
}

File.open("#{Rails.public_path}/statisctic/#{base_file_name}.csv", 'w') { |f| f.write csv }
File.open("#{Rails.public_path}/statisctic/#{base_file_name}.lex", 'w') { |f| f.write lex }
